﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Data.SqlClient;
namespace huaguwang
{
      /// <summary>
      /// 数据访问类:T_MicroBlogging
      /// 创建时间2010年12月31日 09:43:24
      /// 创建人:
      /// </summary>
      /// <remarks>按照公司编码规范,严禁修改此类</remarks>
      public class D_T_MicroBlogging
      {
            public D_T_MicroBlogging()
            { }
            #region  Method
            /// <summary>
            /// 是否存在该记录
            /// 没有flag判断的
            /// </summary>
            public bool Exists(int id)
            {
                  StringBuilder strSql = new StringBuilder();
                  strSql.Append("select count(1) from T_MicroBlogging");
                  strSql.Append(" where id=@id ");
                  SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
                  parameters[0].Value = id;

                  return MSSQLHelper.SQLConType.C1.Exp_Exists(strSql.ToString(), parameters);
            }

            /// <summary>
            /// 是否存在该记录
            /// 带有flag判断的
            /// </summary>
            public bool ExistsFlag(int id)
            {
                  StringBuilder strSql = new StringBuilder();
                  strSql.Append("select count(1) from T_MicroBlogging");
                  strSql.Append(" where id=@id and  Flag=1 ");
                  SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
                  parameters[0].Value = id;

                  return MSSQLHelper.SQLConType.C1.Exp_Exists(strSql.ToString(), parameters);
            }


            /// <summary>
            /// 增加一条数据
            /// </summary>
            public int Add(huaguwang.M_T_MicroBlogging model)
            {
                  StringBuilder strSql = new StringBuilder();
                  strSql.Append("insert into T_MicroBlogging(");
                  strSql.Append("MemberID,Topic,AddDate,IP,TopIs,RemSign,Deblocking,Flag)");
                  strSql.Append(" values (");
                  strSql.Append("@MemberID,@Topic,@AddDate,@IP,@TopIs,@RemSign,@Deblocking,@Flag)");
                  strSql.Append(";select @@IDENTITY");
                  SqlParameter[] parameters = {
					new SqlParameter("@MemberID", SqlDbType.Int,4),
					new SqlParameter("@Topic", SqlDbType.Text),
					new SqlParameter("@AddDate", SqlDbType.SmallDateTime),
					new SqlParameter("@IP", SqlDbType.NVarChar,50),
					new SqlParameter("@TopIs", SqlDbType.Bit,1),
					new SqlParameter("@RemSign", SqlDbType.Bit,1),
					new SqlParameter("@Deblocking", SqlDbType.Bit,1),
					new SqlParameter("@Flag", SqlDbType.Bit,1)};
                  parameters[0].Value = model.MemberID;
                  parameters[1].Value = model.Topic;
                  parameters[2].Value = model.AddDate;
                  parameters[3].Value = model.IP;
                  parameters[4].Value = model.TopIs;
                  parameters[5].Value = model.RemSign;
                  parameters[6].Value = model.Deblocking;
                  //flag标记在增加的使用永远为true
                  parameters[7].Value = true;

                  object obj = MSSQLHelper.SQLConType.C1.Exp_GetSingle(strSql.ToString(), parameters);
                  if (obj == null)
                  {
                        return 0;
                  }
                  else
                  {
                        return Convert.ToInt32(obj);
                  }
            }
            /// <summary>
            /// 更新一条数据
            /// </summary>
            public int Update(huaguwang.M_T_MicroBlogging model)
            {
                  StringBuilder strSql = new StringBuilder();
                  strSql.Append("update T_MicroBlogging set ");
                  strSql.Append("MemberID=@MemberID,");
                  strSql.Append("Topic=@Topic,");
                  strSql.Append("AddDate=@AddDate,");
                  strSql.Append("IP=@IP,");
                  strSql.Append("TopIs=@TopIs,");
                  strSql.Append("RemSign=@RemSign,");
                  strSql.Append("Deblocking=@Deblocking");
                  strSql.Append(" where id=@id");
                  SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@MemberID", SqlDbType.Int,4),
					new SqlParameter("@Topic", SqlDbType.Text),
					new SqlParameter("@AddDate", SqlDbType.SmallDateTime),
					new SqlParameter("@IP", SqlDbType.NVarChar,50),
					new SqlParameter("@TopIs", SqlDbType.Bit,1),
					new SqlParameter("@RemSign", SqlDbType.Bit,1),
					new SqlParameter("@Deblocking", SqlDbType.Bit,1),
					new SqlParameter("@Flag", SqlDbType.Bit,1)};
                  parameters[0].Value = model.id;
                  parameters[1].Value = model.MemberID;
                  parameters[2].Value = model.Topic;
                  parameters[3].Value = model.AddDate;
                  parameters[4].Value = model.IP;
                  parameters[5].Value = model.TopIs;
                  parameters[6].Value = model.RemSign;
                  parameters[7].Value = model.Deblocking;
                  parameters[8].Value = model.Flag;

                  int rows = MSSQLHelper.SQLConType.C1.Exp_ExecuteSql(strSql.ToString(), parameters);
                  if (rows > 0)
                  {
                        return model.id;
                  }
                  else
                  {
                        return 0;
                  }
            }

            /// <summary>
            /// 逻辑删除一条资料
            /// </summary>
            public int UpdateFlag(int id)
            {

                  StringBuilder strSql = new StringBuilder();
                  strSql.Append("update T_MicroBlogging ");
                  strSql.Append(" set flag=0 where id=@id");
                  SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
                  parameters[0].Value = id;

                  int rows = MSSQLHelper.SQLConType.C1.Exp_ExecuteSql(strSql.ToString(), parameters);
                  if (rows > 0)
                  {
                        return id;
                  }
                  else
                  {
                        return 0;
                  }
            }

            /// <summary>
            /// 逻辑删除多条资料
            /// </summary>
            public int UpdateFlag(List<int> list)
            {

                  return MSSQLHelper.SQLConType.C1.Exp_UpdateFlag("T_MicroBlogging", list);
            }


            /// <summary>
            /// 得到一个对象实体
            /// </summary>
            public huaguwang.M_T_MicroBlogging GetModel(int id)
            {

                  StringBuilder strSql = new StringBuilder();
                  strSql.Append("select  top 1 id,MemberID,Topic,AddDate,IP,TopIs,RemSign,Deblocking,Flag from T_MicroBlogging ");
                  strSql.Append(" where id=@id");
                  SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
                  parameters[0].Value = id;

                  huaguwang.M_T_MicroBlogging model = new huaguwang.M_T_MicroBlogging();
                  DataSet ds = MSSQLHelper.SQLConType.C1.Exp_Query(strSql.ToString(), parameters);
                  if (ds.Tables[0].Rows.Count > 0)
                  {
                        if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                        {
                              model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                        }
                        if (ds.Tables[0].Rows[0]["MemberID"].ToString() != "")
                        {
                              model.MemberID = int.Parse(ds.Tables[0].Rows[0]["MemberID"].ToString());
                        }
                        model.Topic = ds.Tables[0].Rows[0]["Topic"].ToString();
                        if (ds.Tables[0].Rows[0]["AddDate"].ToString() != "")
                        {
                              model.AddDate = DateTime.Parse(ds.Tables[0].Rows[0]["AddDate"].ToString());
                        }
                        model.IP = ds.Tables[0].Rows[0]["IP"].ToString();
                        if (ds.Tables[0].Rows[0]["TopIs"].ToString() != "")
                        {
                              if ((ds.Tables[0].Rows[0]["TopIs"].ToString() == "1") || (ds.Tables[0].Rows[0]["TopIs"].ToString().ToLower() == "true"))
                              {
                                    model.TopIs = true;
                              }
                              else
                              {
                                    model.TopIs = false;
                              }
                        }
                        if (ds.Tables[0].Rows[0]["RemSign"].ToString() != "")
                        {
                              if ((ds.Tables[0].Rows[0]["RemSign"].ToString() == "1") || (ds.Tables[0].Rows[0]["RemSign"].ToString().ToLower() == "true"))
                              {
                                    model.RemSign = true;
                              }
                              else
                              {
                                    model.RemSign = false;
                              }
                        }
                        if (ds.Tables[0].Rows[0]["Deblocking"].ToString() != "")
                        {
                              if ((ds.Tables[0].Rows[0]["Deblocking"].ToString() == "1") || (ds.Tables[0].Rows[0]["Deblocking"].ToString().ToLower() == "true"))
                              {
                                    model.Deblocking = true;
                              }
                              else
                              {
                                    model.Deblocking = false;
                              }
                        }
                        if (ds.Tables[0].Rows[0]["Flag"].ToString() != "")
                        {
                              if ((ds.Tables[0].Rows[0]["Flag"].ToString() == "1") || (ds.Tables[0].Rows[0]["Flag"].ToString().ToLower() == "true"))
                              {
                                    model.Flag = true;
                              }
                              else
                              {
                                    model.Flag = false;
                              }
                        }
                        return model;
                  }
                  else
                  {
                        return null;
                  }
            }


            /// <summary>
            /// 得到一个对象实体
            /// 带有flag标签条件的
            /// </summary>
            public huaguwang.M_T_MicroBlogging GetModelFlag(int id)
            {

                  StringBuilder strSql = new StringBuilder();
                  strSql.Append("select  top 1 id,MemberID,Topic,AddDate,IP,TopIs,RemSign,Deblocking,Flag from T_MicroBlogging ");
                  strSql.Append(" where id=@id and Flag=1 ");
                  SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
                  parameters[0].Value = id;

                  huaguwang.M_T_MicroBlogging model = new huaguwang.M_T_MicroBlogging();
                  DataSet ds = MSSQLHelper.SQLConType.C1.Exp_Query(strSql.ToString(), parameters);
                  if (ds.Tables[0].Rows.Count > 0)
                  {
                        if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                        {
                              model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                        }
                        if (ds.Tables[0].Rows[0]["MemberID"].ToString() != "")
                        {
                              model.MemberID = int.Parse(ds.Tables[0].Rows[0]["MemberID"].ToString());
                        }
                        model.Topic = ds.Tables[0].Rows[0]["Topic"].ToString();
                        if (ds.Tables[0].Rows[0]["AddDate"].ToString() != "")
                        {
                              model.AddDate = DateTime.Parse(ds.Tables[0].Rows[0]["AddDate"].ToString());
                        }
                        model.IP = ds.Tables[0].Rows[0]["IP"].ToString();
                        if (ds.Tables[0].Rows[0]["TopIs"].ToString() != "")
                        {
                              if ((ds.Tables[0].Rows[0]["TopIs"].ToString() == "1") || (ds.Tables[0].Rows[0]["TopIs"].ToString().ToLower() == "true"))
                              {
                                    model.TopIs = true;
                              }
                              else
                              {
                                    model.TopIs = false;
                              }
                        }
                        if (ds.Tables[0].Rows[0]["RemSign"].ToString() != "")
                        {
                              if ((ds.Tables[0].Rows[0]["RemSign"].ToString() == "1") || (ds.Tables[0].Rows[0]["RemSign"].ToString().ToLower() == "true"))
                              {
                                    model.RemSign = true;
                              }
                              else
                              {
                                    model.RemSign = false;
                              }
                        }
                        if (ds.Tables[0].Rows[0]["Deblocking"].ToString() != "")
                        {
                              if ((ds.Tables[0].Rows[0]["Deblocking"].ToString() == "1") || (ds.Tables[0].Rows[0]["Deblocking"].ToString().ToLower() == "true"))
                              {
                                    model.Deblocking = true;
                              }
                              else
                              {
                                    model.Deblocking = false;
                              }
                        }
                        if (ds.Tables[0].Rows[0]["Flag"].ToString() != "")
                        {
                              if ((ds.Tables[0].Rows[0]["Flag"].ToString() == "1") || (ds.Tables[0].Rows[0]["Flag"].ToString().ToLower() == "true"))
                              {
                                    model.Flag = true;
                              }
                              else
                              {
                                    model.Flag = false;
                              }
                        }
                        return model;
                  }
                  else
                  {
                        return null;
                  }
            }

            /// <summary>
            /// 获取所有资料,如果没有资料不会返回null 
            /// </summary>
            public List<huaguwang.M_T_MicroBlogging> GetList(string strWhere, SqlParameter[] parameters)
            {
                  StringBuilder strSql = new StringBuilder();
                  strSql.Append("select id,MemberID,Topic,AddDate,IP,TopIs,RemSign,Deblocking,Flag ");
                  strSql.Append(" FROM T_MicroBlogging where flag=1 ");
                  if (strWhere.Trim() != "")
                  {
                        strSql.Append(" and " + strWhere);
                  }
                  var list = MSSQLHelper.SQLConType.C1.Exp_GetList(strSql.ToString(),
                  dr => DataRowToModel(dr), parameters);
                  return (list.Count > 0) ? list : new List<huaguwang.M_T_MicroBlogging>();
            }

            /// <summary>
            /// 获取单表的分页资料 
            /// </summary>
            public List<huaguwang.M_T_MicroBlogging> GetOneTablePage(int Begin, int Page, string strWhere, SqlParameter[] parameters)
            {
                  if (strWhere.Exp_Trim().Length > 0)
                  {
                        strWhere = " a.flag=1 and " + strWhere;
                  }
                  else
                  {
                        strWhere = " a.flag=1  ";
                  }
                  string strSQL = ExpandMSSQL.GetPageSQL(
                        Begin,
                        Page,
                        "a.id,a.MemberID,a.Topic,a.AddDate,a.IP,a.TopIs,a.RemSign,a.Deblocking,a.Flag",
                        "T_MicroBlogging as a ",
                        "a.id",
                        strWhere,
                        "a.id  desc");
                  var list = MSSQLHelper.SQLConType.C1.Exp_GetList(strSQL,
                  dr => DataRowToModel(dr), parameters);
                  return (list.Count > 0) ? list : new List<huaguwang.M_T_MicroBlogging>();
            }

            /// <summary>
            /// 获取单表的分页资料 
            /// </summary>
            public int GetOneTablePageCount(string strWhere, SqlParameter[] parameters)
            {
                  if (strWhere.Exp_Trim().Length > 0)
                  {
                        strWhere = " a.flag=1 and " + strWhere;
                  }
                  else
                  {
                        strWhere = " a.flag=1  ";
                  }
                  string strSQL = ExpandMSSQL.GetPageSQL(
                        "T_MicroBlogging as a ",
                        strWhere);
                  return MSSQLHelper.SQLConType.C1.Exp_GetSingle(strSQL, parameters).Exp_IntTryParse();
            }

            /// <summary>
            /// 把dr转换成model的方法
            /// </summary>
            public huaguwang.M_T_MicroBlogging DataRowToModel(DataRow dr)
            {
                  var model = new huaguwang.M_T_MicroBlogging();
                  if (dr["id"].ToString() != "")
                  {
                        model.id = int.Parse(dr["id"].ToString());
                  }
                  if (dr["MemberID"].ToString() != "")
                  {
                        model.MemberID = int.Parse(dr["MemberID"].ToString());
                  }
                  model.Topic = dr["Topic"].ToString();
                  if (dr["AddDate"].ToString() != "")
                  {
                        model.AddDate = DateTime.Parse(dr["AddDate"].ToString());
                  }
                  model.IP = dr["IP"].ToString();
                  if (dr["TopIs"].ToString() != "")
                  {
                        if ((dr["TopIs"].ToString() == "1") || (dr["TopIs"].ToString().ToLower() == "true"))
                        {
                              model.TopIs = true;
                        }
                        else
                        {
                              model.TopIs = false;
                        }
                  }
                  if (dr["RemSign"].ToString() != "")
                  {
                        if ((dr["RemSign"].ToString() == "1") || (dr["RemSign"].ToString().ToLower() == "true"))
                        {
                              model.RemSign = true;
                        }
                        else
                        {
                              model.RemSign = false;
                        }
                  }
                  if (dr["Deblocking"].ToString() != "")
                  {
                        if ((dr["Deblocking"].ToString() == "1") || (dr["Deblocking"].ToString().ToLower() == "true"))
                        {
                              model.Deblocking = true;
                        }
                        else
                        {
                              model.Deblocking = false;
                        }
                  }
                  if (dr["Flag"].ToString() != "")
                  {
                        if ((dr["Flag"].ToString() == "1") || (dr["Flag"].ToString().ToLower() == "true"))
                        {
                              model.Flag = true;
                        }
                        else
                        {
                              model.Flag = false;
                        }
                  }
                  return model;
            }

            #endregion  Method
      }
}

